# -*- coding: utf-8 -*-

# PROTOTYPE, NOT TEST-COVERED!

class editor_logic_state_typing_new_kb_type :
    def __init__ ( self , mediator ) :
        self . _mediator = mediator
        self . _awake = False
        self . _prev_string = ''
        self . _key_space = False
        self . _key_return = False
    def activate_editor_state_typing_new_kb_type ( self ) :
        self . _prev_string = ''
        self . _awake = True
    def hide_editor ( self ) :
        self . _awake = False
    def receive_editor_entry_string ( self , entry_string ) :
        if self . _awake :
            invalid_name = False
            for ch in entry_string :
                if ch in ' !§@#$%^&*(),.;\'"[]{}/?<>-=+|' :
                    invalid_name = True
                    break
            if invalid_name :
                self . _mediator . set_editor_status_string ( 'invalid new entry name: "%s"' % entry_string )
                self . _mediator . set_editor_content_entry_string ( self . _prev_string )
            else :
                self . _prev_string = entry_string
    def receive_is_kb_type_stored ( self , is_stored ) :
        if self . _awake :
            if self . _key_space :
                self . _key_space = False
                if is_stored :
                    self . _mediator . set_editor_status_string ( 'entry with this name already exists' )
                    self . _mediator . set_editor_content_entry_string ( self . _prev_string )
                elif len ( self . _prev_string ) == 0 :
                    self . _mediator . set_editor_status_string ( 'entry name must not be empty' )
                    self . _mediator . set_editor_content_entry_string ( '' )
                else :
                    self . _mediator . store_kb_type ( self . _prev_string )
                    self . _mediator . delete_entry_from_editor_contents ( )
                    self . _mediator . add_text_to_editor_contents ( self . _prev_string )
                    self . _mediator . set_editor_status_string ( 'new entry created' )
                    self . _awake = False
                    self . _mediator . activate_editor_state_editing_query ( )
            if self . _key_return :
                self . _key_return = False
                if is_stored :
                    self . _mediator . set_editor_status_string ( 'entry with this name already exists' )
                elif len ( self . _prev_string ) == 0 :
                    self . _mediator . set_editor_status_string ( 'entry name must not be empty' )
                else :
                    self . _mediator . remove_all_marks_from_editor_contents ( )
                    self . _mediator . store_kb_type ( self . _prev_string )
                    self . _mediator . delete_entry_from_editor_contents ( )
                    self . _mediator . add_text_to_editor_contents ( self . _prev_string + ';\n' )
                    self . _mediator . set_editor_status_string ( 'new entry created and query finished' )
                    self . _awake = False
                    self . _mediator . activate_editor_state_creating_new_query ( )
    def receive_is_any_marks_left_in_editor_contents ( self , any_marks ) :
        if self . _awake :
            self . _awake = False
            if any_marks :
                self . _mediator . activate_editor_state_editing_query ( )
            else :
                self . _mediator . activate_editor_state_creating_new_query ( )
    def on_any_key ( self ) :
        if self . _awake :
            self . _mediator . request_editor_entry_string ( )
    def on_key_backspace ( self ) :
        if self . _awake :
            self . _mediator . request_editor_entry_string ( )
    def on_key_space ( self ) :
        if self . _awake :
            self . _key_space = True
            self . _mediator . request_is_kb_type_stored ( self . _prev_string )
    def on_key_return ( self ) :
        if self . _awake :
            self . _key_return = True
            self . _mediator . request_is_kb_type_stored ( self . _prev_string )
    def on_key_escape ( self ) :
        if self . _awake :
            self . _mediator . set_editor_status_string ( 'new entry creation cancelled' )
            self . _mediator . delete_entry_from_editor_contents ( )
            self . _mediator . delete_text_from_last_mark_to_end_of_editor_contents ( )
            self . _mediator . request_is_any_marks_left_in_editor_contents ( )
